package tk.kolesnikov.db;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class DBSettingsParser extends DefaultHandler{

    private DBSettings settings = new DBSettings();
    private String currentElement = "";

    public DBSettings getSettings() {
        return settings;
    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        this.currentElement = qName;
    }

    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        this.currentElement = "";
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        if (this.currentElement.equals("name")) {
            settings.setDbName(new String(ch, start, length));
        }
        if (this.currentElement.equals("driver")) {
            settings.setDriverName(new String(ch, start, length));
        }
        if (this.currentElement.equals("url")) {
            settings.setUrl(new String(ch, start, length));
        }
        if (this.currentElement.equals("login")) {
            settings.setLogin(new String(ch, start, length));
        }
        if (this.currentElement.equals("password")) {
            settings.setPassword(new String(ch, start, length));
        }
    }

}
